package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleResourceService;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceService roleResourceService;

    @Override
    public PageResponse<RoleVo> findByPage(Integer pageNum, Integer pageSize, RoleDto dto) {
        //使用分页工具
        PageHelper.startPage(pageNum,pageSize);
        //获取角色名称 唯一
        String roleName = dto.getRoleName();
        //查询角色
        List<Role> roles = roleMapper.selectByPage(roleName);
        //强转Page
        Page<Role> page = (Page<Role>) roles;
        //返回
        return PageResponse.of(page, RoleVo.class);
    }

    @Override
    public void addRole(RoleDto dto) {
        //强转为Role
        Role role = BeanUtil.toBean(dto, Role.class);
        //添加角色到角色表
        roleMapper.insert(role);
    }

    @Override
    public List<String> findCheckedResources(Long roleId) {
        List<String> stringList = roleResourceService.findCheckedResources(roleId);
        return stringList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleAndResources(RoleDto dto) {
        //判断是更新角色名称还是角色对应的资源
        if (StringUtils.isNotBlank(dto.getRoleName())){
            //角色名称不为空 更新角色名称
            //强转Role
            Role role = BeanUtil.toBean(dto, Role.class);
            role.setRoleName(dto.getRoleName());
            role.setDataState(dto.getDataState());
            roleMapper.updateByPrimaryKey(role);
        }else {
            //更新角色资源
            //获取资源表已有资源
            String[] resourceNos = dto.getCheckedResourceNos();
            //判断资源是否为空
            if (resourceNos.length == 0 || resourceNos == null){
                return;
            }
            //创建一个集合存roleResource对象
            List<RoleResource> list = new ArrayList<>();
            for (String no : resourceNos) {
                //创建一个RoleResource对象
                RoleResource roleResource = new RoleResource(dto.getId(), no, "0");
                //添加到集合
                list.add(roleResource);
            }
            //资源不为空 删除资源表的数据
            roleResourceService.deleteByPrimaryKey(dto.getId());
            //添加修改后的资源数据
            roleResourceService.batchInsert(list);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleById(Long roleId) {
        //删除角色
        roleMapper.deleteByPrimaryKey(roleId);
        //删除角色对应的资源
        roleResourceService.deleteByPrimaryKey(roleId);
    }

    /***
     *  角色下拉框
     * @return
     */
    @Override
    public List<RoleVo> initRoles() {
        List<Role> roleList = roleMapper.initRoles();
        return BeanUtil.copyToList(roleList,RoleVo.class);
    }

}
